package com.example.workflow.security;


import com.example.workflow.pojo.Result;
import com.example.workflow.util.WebUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
            log.info("认证失败，请重新登录");
//            ObjectMapper objectMapper = new ObjectMapper();
//            Result<String> result = new Result<String>(HttpStatus.UNAUTHORIZED.value(), "认证失败请重新登录", "认证失败请重新登录");
//            String json = objectMapper.writeValueAsString(result);
//            WebUtils.renderString(response, json);
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("{\"code\":\"401\",\"message\":\"认证失败，请重新登录\",\"data\":null}");
    }
}


